<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter"
                :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="search">
        <el-form ref="queryFormRef" :inline="true" :model="queryParams" label-width="68px">
          <el-form-item label="订单编号" prop="orderNo">
            <el-input v-model="queryParams.orderNo" clearable placeholder="请输入订单编号" style="width: 240px"
                      @keyup.enter="handleQuery"/>
          </el-form-item>
          <el-form-item label="第三方订单编号" prop="thirdOrderNo">
            <el-input v-model="queryParams.thirdOrderNo" clearable placeholder="请输入第三方订单编号"
                      style="width: 240px" @keyup.enter="handleQuery"/>
          </el-form-item>
          <el-form-item label="快递公司" prop="deviceType">
            <el-select v-model="queryParams.deviceType" clearable placeholder="请选择快递公司">
              <el-option
                v-for="dict in kd_delivery_company"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="寄件渠道" prop="customerType">
            <el-select v-model="queryParams.customerType" clearable placeholder="请选择寄件渠道">
              <el-option
                v-for="dict in kd_customer_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="是否线上付" prop="onlinePay">
            <el-select v-model="queryParams.onlinePay" clearable placeholder="请选择是否线上付">
              <el-option
                v-for="dict in kd_online_pay"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="支付方式" prop="payMethod">
            <el-select v-model="queryParams.payMethod" clearable placeholder="请选择支付方式">
              <el-option
                v-for="dict in kd_pay_method"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="订单状态" prop="status">
            <el-select v-model="queryParams.status" clearable placeholder="请选择订单状态">
              <el-option
                v-for="dict in kd_order_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="运单号" prop="deliveryId">
            <el-input v-model="queryParams.deliveryId" clearable placeholder="请输入运单号" style="width: 240px"
                      @keyup.enter="handleQuery"/>
          </el-form-item>
          <el-form-item>
            <el-button icon="Search" type="primary" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <!--          <el-col :span="1.5">-->
          <!--            <el-button v-hasPermi="['kd:order:add']" icon="Plus" plain type="primary" @click="handleAdd">新增-->
          <!--            </el-button>-->
          <!--          </el-col>-->
          <!--          <el-col :span="1.5">-->
          <!--            <el-button v-hasPermi="['kd:order:edit']" :disabled="single" icon="Edit" plain type="success"-->
          <!--                       @click="handleUpdate()">修改-->
          <!--            </el-button>-->
          <!--          </el-col>-->
          <el-col :span="1.5">
            <el-button v-hasPermi="['kd:order:remove']" :disabled="multiple" icon="Delete" plain type="danger"
                       @click="handleDelete()">删除
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['kd:order:export']" icon="Download" plain type="warning" @click="handleExport">
              导出
            </el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
        <el-table-column align="center" type="selection" width="55"/>
        <!--        <el-table-column v-if="true" align="center" label="ID" prop="id"/>-->
        <el-table-column align="center" label="订单编号" prop="orderNo"/>
        <el-table-column align="center" label="第三方订单编号" prop="thirdOrderNo"/>
        <el-table-column align="center" label="快递公司" prop="deviceType">
          <template #default="scope">
            <dict-tag :options="kd_delivery_company" :value="scope.row.deviceType"/>
          </template>
        </el-table-column>
        <el-table-column align="center" label="寄件渠道" prop="customerType">
          <template #default="scope">
            <dict-tag :options="kd_customer_type" :value="scope.row.customerType"/>
          </template>
        </el-table-column>
        <el-table-column align="center" label="产品类型" prop="deliveryBusiness"/>
        <el-table-column align="center" label="指定下单渠道ID" prop="channelId"/>
        <el-table-column align="center" label="发件省" prop="senderProvince"/>
        <el-table-column align="center" label="发件市" prop="senderCity"/>
        <el-table-column align="center" label="发件区" prop="senderDistrict"/>
        <el-table-column align="center" label="发件人姓名" prop="senderName"/>
        <el-table-column align="center" label="发件详细地址" prop="senderAddress"/>
        <el-table-column align="center" label="发件人电话" prop="senderTel"/>
        <el-table-column align="center" label="发件人手机" prop="senderMobile"/>
        <el-table-column align="center" label="收件省" prop="receiveProvince"/>
        <el-table-column align="center" label="收件市" prop="receiveCity"/>
        <el-table-column align="center" label="收件区" prop="receiveDistrict"/>
        <el-table-column align="center" label="收件人姓名" prop="receiveName"/>
        <el-table-column align="center" label="收件详细地址" prop="receiveAddress"/>
        <el-table-column align="center" label="收件人电话" prop="receiveTel"/>
        <el-table-column align="center" label="收件人手机" prop="receiveMobile"/>
        <el-table-column align="center" label="托寄物名称" prop="goods"/>
        <el-table-column align="center" label="包裹数" prop="packageCount"/>
        <el-table-column align="center" label="重量(单位：kg，保留小数点后两位)" prop="weight"/>
        <el-table-column align="center" label="长/CM" prop="vloumLong"/>
        <el-table-column align="center" label="宽/CM" prop="vloumWidth"/>
        <el-table-column align="center" label="高/CM" prop="vloumHeight"/>
        <el-table-column align="center" label="保价金额 保价上限30w元" prop="guaranteeValueAmount"/>
        <el-table-column align="center" label="预约取件时间开始" prop="pickUpStartTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.pickUpStartTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="预约取件时间结束" prop="pickUpEndTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.pickUpEndTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="备注" prop="remark"/>
        <el-table-column align="center" label="是否线上付" prop="onlinePay">
          <template #default="scope">
            <dict-tag :options="kd_online_pay" :value="scope.row.onlinePay"/>
          </template>
        </el-table-column>
        <el-table-column align="center" label="支付方式" prop="payMethod">
          <template #default="scope">
            <dict-tag :options="kd_pay_method" :value="scope.row.payMethod"/>
          </template>
        </el-table-column>
        <el-table-column align="center" label="附加服务" prop="orderIncrements"/>
        <el-table-column align="center" label="订单状态" prop="status">
          <template #default="scope">
            <dict-tag :options="kd_order_status" :value="scope.row.status"/>
          </template>
        </el-table-column>
        <el-table-column align="center" label="实际重量 (KG)" prop="realWeight"/>
        <el-table-column align="center" label="实际体积 (CM³)" prop="realVolume"/>
        <el-table-column align="center" label="计费重量" prop="calcFeeWeight"/>
        <el-table-column align="center" label="是否返佣 Y/N" prop="needRebate"/>
        <el-table-column align="center" label="费用明细" prop="feeBlockDetail"/>
        <el-table-column align="center" label="取件人" prop="courierName"/>
        <el-table-column align="center" label="取件人联系方式" prop="courierPhone"/>
        <el-table-column align="center" label="取件码" prop="pickUpCode"/>
        <el-table-column align="center" label="站点" prop="siteName"/>
        <el-table-column align="center" label="运单号" prop="deliveryId"/>
        <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button v-hasPermi="['kd:order:edit']" icon="Edit" link type="primary"
                         @click="handleUpdate(scope.row)"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button v-hasPermi="['kd:order:remove']" icon="Delete" link type="primary"
                         @click="handleDelete(scope.row)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        v-model:limit="queryParams.pageSize"
        v-model:page="queryParams.pageNum"
        :total="total"
        @pagination="getList"
      />
    </el-card>
    <!-- 添加或修改快递订单对话框 -->
    <el-dialog v-model="dialog.visible" :title="dialog.title" append-to-body width="500px">
      <el-form ref="orderFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单编号" prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入订单编号"/>
        </el-form-item>
        <el-form-item label="第三方订单编号" prop="thirdOrderNo">
          <el-input v-model="form.thirdOrderNo" placeholder="请输入第三方订单编号"/>
        </el-form-item>
        <el-form-item label="快递公司" prop="deviceType">
          <el-select v-model="form.deviceType" placeholder="请选择快递公司">
            <el-option
              v-for="dict in kd_delivery_company"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="寄件渠道" prop="customerType">
          <el-select v-model="form.customerType" placeholder="请选择寄件渠道">
            <el-option
              v-for="dict in kd_customer_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="指定下单渠道ID" prop="channelId">
          <el-input v-model="form.channelId" placeholder="请输入指定下单渠道ID"/>
        </el-form-item>
        <el-form-item label="发件省" prop="senderProvince">
          <el-input v-model="form.senderProvince" placeholder="请输入发件省"/>
        </el-form-item>
        <el-form-item label="发件市" prop="senderCity">
          <el-input v-model="form.senderCity" placeholder="请输入发件市"/>
        </el-form-item>
        <el-form-item label="发件区" prop="senderDistrict">
          <el-input v-model="form.senderDistrict" placeholder="请输入发件区"/>
        </el-form-item>
        <el-form-item label="发件人姓名" prop="senderName">
          <el-input v-model="form.senderName" placeholder="请输入发件人姓名"/>
        </el-form-item>
        <el-form-item label="发件详细地址" prop="senderAddress">
          <el-input v-model="form.senderAddress" placeholder="请输入发件详细地址"/>
        </el-form-item>
        <el-form-item label="发件人电话" prop="senderTel">
          <el-input v-model="form.senderTel" placeholder="请输入发件人电话"/>
        </el-form-item>
        <el-form-item label="发件人手机" prop="senderMobile">
          <el-input v-model="form.senderMobile" placeholder="请输入发件人手机"/>
        </el-form-item>
        <el-form-item label="收件省" prop="receiveProvince">
          <el-input v-model="form.receiveProvince" placeholder="请输入收件省"/>
        </el-form-item>
        <el-form-item label="收件市" prop="receiveCity">
          <el-input v-model="form.receiveCity" placeholder="请输入收件市"/>
        </el-form-item>
        <el-form-item label="收件区" prop="receiveDistrict">
          <el-input v-model="form.receiveDistrict" placeholder="请输入收件区"/>
        </el-form-item>
        <el-form-item label="收件人姓名" prop="receiveName">
          <el-input v-model="form.receiveName" placeholder="请输入收件人姓名"/>
        </el-form-item>
        <el-form-item label="收件详细地址" prop="receiveAddress">
          <el-input v-model="form.receiveAddress" placeholder="请输入收件详细地址"/>
        </el-form-item>
        <el-form-item label="收件人电话" prop="receiveTel">
          <el-input v-model="form.receiveTel" placeholder="请输入收件人电话"/>
        </el-form-item>
        <el-form-item label="收件人手机" prop="receiveMobile">
          <el-input v-model="form.receiveMobile" placeholder="请输入收件人手机"/>
        </el-form-item>
        <el-form-item label="托寄物名称" prop="goods">
          <el-input v-model="form.goods" placeholder="请输入托寄物名称"/>
        </el-form-item>
        <el-form-item label="包裹数" prop="packageCount">
          <el-input v-model="form.packageCount" placeholder="请输入包裹数"/>
        </el-form-item>
        <el-form-item label="重量(单位：kg，保留小数点后两位)" prop="weight">
          <el-input v-model="form.weight" placeholder="请输入重量(单位：kg，保留小数点后两位)"/>
        </el-form-item>
        <el-form-item label="长/CM" prop="vloumLong">
          <el-input v-model="form.vloumLong" placeholder="请输入长/CM"/>
        </el-form-item>
        <el-form-item label="宽/CM" prop="vloumWidth">
          <el-input v-model="form.vloumWidth" placeholder="请输入宽/CM"/>
        </el-form-item>
        <el-form-item label="高/CM" prop="vloumHeight">
          <el-input v-model="form.vloumHeight" placeholder="请输入高/CM"/>
        </el-form-item>
        <el-form-item label="保价金额 保价上限30w元" prop="guaranteeValueAmount">
          <el-input v-model="form.guaranteeValueAmount" placeholder="请输入保价金额 保价上限30w元"/>
        </el-form-item>
        <el-form-item label="预约取件时间开始" prop="pickUpStartTime">
          <el-date-picker v-model="form.pickUpStartTime"
                          clearable
                          placeholder="请选择预约取件时间开始"
                          type="datetime"
                          value-format="YYYY-MM-DD HH:mm:ss">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="预约取件时间结束" prop="pickUpEndTime">
          <el-date-picker v-model="form.pickUpEndTime"
                          clearable
                          placeholder="请选择预约取件时间结束"
                          type="datetime"
                          value-format="YYYY-MM-DD HH:mm:ss">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>
        <el-form-item label="是否线上付" prop="onlinePay">
          <el-select v-model="form.onlinePay" placeholder="请选择是否线上付">
            <el-option
              v-for="dict in kd_online_pay"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="支付方式" prop="payMethod">
          <el-select v-model="form.payMethod" placeholder="请选择支付方式">
            <el-option
              v-for="dict in kd_pay_method"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="附加服务" prop="orderIncrements">
          <el-input v-model="form.orderIncrements" placeholder="请输入附加服务"/>
        </el-form-item>
        <el-form-item label="订单状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择订单状态">
            <el-option
              v-for="dict in kd_order_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="实际重量 (KG)" prop="realWeight">
          <el-input v-model="form.realWeight" placeholder="请输入实际重量 (KG)"/>
        </el-form-item>
        <el-form-item label="实际体积 (CM³)" prop="realVolume">
          <el-input v-model="form.realVolume" placeholder="请输入实际体积 (CM³)"/>
        </el-form-item>
        <el-form-item label="计费重量" prop="calcFeeWeight">
          <el-input v-model="form.calcFeeWeight" placeholder="请输入计费重量"/>
        </el-form-item>
        <el-form-item label="是否返佣 Y/N" prop="needRebate">
          <el-input v-model="form.needRebate" placeholder="请输入是否返佣 Y/N"/>
        </el-form-item>
        <el-form-item label="费用明细" prop="feeBlockDetail">
          <el-input v-model="form.feeBlockDetail" placeholder="请输入费用明细"/>
        </el-form-item>
        <el-form-item label="取件人" prop="courierName">
          <el-input v-model="form.courierName" placeholder="请输入取件人"/>
        </el-form-item>
        <el-form-item label="取件人联系方式" prop="courierPhone">
          <el-input v-model="form.courierPhone" placeholder="请输入取件人联系方式"/>
        </el-form-item>
        <el-form-item label="取件码" prop="pickUpCode">
          <el-input v-model="form.pickUpCode" placeholder="请输入取件码"/>
        </el-form-item>
        <el-form-item label="站点" prop="siteName">
          <el-input v-model="form.siteName" placeholder="请输入站点"/>
        </el-form-item>
        <el-form-item label="运单号" prop="deliveryId">
          <el-input v-model="form.deliveryId" placeholder="请输入运单号"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" name="Order" setup>
import {addOrder, delOrder, getOrder, listOrder, updateOrder} from '@/api/kd/order';
import {OrderForm, OrderQuery, OrderVO} from '@/api/kd/order/types';

const {proxy} = getCurrentInstance() as ComponentInternalInstance;
const {
  kd_delivery_company,
  kd_pay_method,
  kd_order_status,
  kd_customer_type,
  kd_online_pay
} = toRefs<any>(proxy?.useDict('kd_delivery_company', 'kd_pay_method', 'kd_order_status', 'kd_customer_type', 'kd_online_pay'));

const orderList = ref<OrderVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const orderFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: OrderForm = {
  id: undefined,
  orderNo: undefined,
  thirdOrderNo: undefined,
  deviceType: undefined,
  customerType: undefined,
  deliveryBusiness: undefined,
  channelId: undefined,
  senderProvince: undefined,
  senderCity: undefined,
  senderDistrict: undefined,
  senderName: undefined,
  senderAddress: undefined,
  senderTel: undefined,
  senderMobile: undefined,
  receiveProvince: undefined,
  receiveCity: undefined,
  receiveDistrict: undefined,
  receiveName: undefined,
  receiveAddress: undefined,
  receiveTel: undefined,
  receiveMobile: undefined,
  goods: undefined,
  packageCount: undefined,
  weight: undefined,
  vloumLong: undefined,
  vloumWidth: undefined,
  vloumHeight: undefined,
  guaranteeValueAmount: undefined,
  pickUpStartTime: undefined,
  pickUpEndTime: undefined,
  remark: undefined,
  onlinePay: undefined,
  payMethod: undefined,
  orderIncrements: undefined,
  status: undefined,
  realWeight: undefined,
  realVolume: undefined,
  calcFeeWeight: undefined,
  needRebate: undefined,
  feeBlockDetail: undefined,
  courierName: undefined,
  courierPhone: undefined,
  pickUpCode: undefined,
  siteName: undefined,
  deliveryId: undefined
}
const data = reactive<PageData<OrderForm, OrderQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderNo: undefined,
    thirdOrderNo: undefined,
    deviceType: undefined,
    customerType: undefined,
    deliveryBusiness: undefined,
    onlinePay: undefined,
    payMethod: undefined,
    status: undefined,
    deliveryId: undefined,
    params: {}
  },
  rules: {
    id: [
      {required: true, message: "ID不能为空", trigger: "blur"}
    ],
    orderNo: [
      {required: true, message: "订单编号不能为空", trigger: "blur"}
    ],
    thirdOrderNo: [
      {required: true, message: "第三方订单编号不能为空", trigger: "blur"}
    ],
    deviceType: [
      {required: true, message: "快递公司不能为空", trigger: "change"}
    ],
    customerType: [
      {required: true, message: "寄件渠道不能为空", trigger: "change"}
    ],
    deliveryBusiness: [
      {required: true, message: "产品类型不能为空", trigger: "change"}
    ],
    channelId: [
      {required: true, message: "指定下单渠道ID不能为空", trigger: "blur"}
    ],
    senderProvince: [
      {required: true, message: "发件省不能为空", trigger: "blur"}
    ],
    senderCity: [
      {required: true, message: "发件市不能为空", trigger: "blur"}
    ],
    senderDistrict: [
      {required: true, message: "发件区不能为空", trigger: "blur"}
    ],
    senderName: [
      {required: true, message: "发件人姓名不能为空", trigger: "blur"}
    ],
    senderAddress: [
      {required: true, message: "发件详细地址不能为空", trigger: "blur"}
    ],
    senderTel: [
      {required: true, message: "发件人电话不能为空", trigger: "blur"}
    ],
    senderMobile: [
      {required: true, message: "发件人手机不能为空", trigger: "blur"}
    ],
    receiveProvince: [
      {required: true, message: "收件省不能为空", trigger: "blur"}
    ],
    receiveCity: [
      {required: true, message: "收件市不能为空", trigger: "blur"}
    ],
    receiveDistrict: [
      {required: true, message: "收件区不能为空", trigger: "blur"}
    ],
    receiveName: [
      {required: true, message: "收件人姓名不能为空", trigger: "blur"}
    ],
    receiveAddress: [
      {required: true, message: "收件详细地址不能为空", trigger: "blur"}
    ],
    receiveTel: [
      {required: true, message: "收件人电话不能为空", trigger: "blur"}
    ],
    receiveMobile: [
      {required: true, message: "收件人手机不能为空", trigger: "blur"}
    ],
    goods: [
      {required: true, message: "托寄物名称不能为空", trigger: "blur"}
    ],
    packageCount: [
      {required: true, message: "包裹数不能为空", trigger: "blur"}
    ],
    weight: [
      {required: true, message: "重量(单位：kg，保留小数点后两位)不能为空", trigger: "blur"}
    ],
    vloumLong: [
      {required: true, message: "长/CM不能为空", trigger: "blur"}
    ],
    vloumWidth: [
      {required: true, message: "宽/CM不能为空", trigger: "blur"}
    ],
    vloumHeight: [
      {required: true, message: "高/CM不能为空", trigger: "blur"}
    ],
    guaranteeValueAmount: [
      {required: true, message: "保价金额 保价上限30w元不能为空", trigger: "blur"}
    ],
    pickUpStartTime: [
      {required: true, message: "预约取件时间开始不能为空", trigger: "blur"}
    ],
    pickUpEndTime: [
      {required: true, message: "预约取件时间结束不能为空", trigger: "blur"}
    ],
    remark: [
      {required: true, message: "备注不能为空", trigger: "blur"}
    ],
    onlinePay: [
      {required: true, message: "是否线上付不能为空", trigger: "change"}
    ],
    payMethod: [
      {required: true, message: "支付方式不能为空", trigger: "change"}
    ],
    orderIncrements: [
      {required: true, message: "附加服务不能为空", trigger: "blur"}
    ],
    status: [
      {required: true, message: "订单状态不能为空", trigger: "change"}
    ],
    realWeight: [
      {required: true, message: "实际重量 (KG)不能为空", trigger: "blur"}
    ],
    realVolume: [
      {required: true, message: "实际体积 (CM³)不能为空", trigger: "blur"}
    ],
    calcFeeWeight: [
      {required: true, message: "计费重量不能为空", trigger: "blur"}
    ],
    needRebate: [
      {required: true, message: "是否返佣 Y/N不能为空", trigger: "blur"}
    ],
    feeBlockDetail: [
      {required: true, message: "费用明细不能为空", trigger: "blur"}
    ],
    courierName: [
      {required: true, message: "取件人不能为空", trigger: "blur"}
    ],
    courierPhone: [
      {required: true, message: "取件人联系方式不能为空", trigger: "blur"}
    ],
    pickUpCode: [
      {required: true, message: "取件码不能为空", trigger: "blur"}
    ],
    siteName: [
      {required: true, message: "站点不能为空", trigger: "blur"}
    ],
    deliveryId: [
      {required: true, message: "运单号不能为空", trigger: "blur"}
    ]
  }
});

const {queryParams, form, rules} = toRefs(data);

/** 查询快递订单列表 */
const getList = async () => {
  loading.value = true;
  const res = await listOrder(queryParams.value);
  orderList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  orderFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: OrderVO[]) => {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = "添加快递订单";
}

/** 修改按钮操作 */
const handleUpdate = async (row?: OrderVO) => {
  reset();
  const _id = row?.id || ids.value[0]
  const res = await getOrder(_id);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = "修改快递订单";
}

/** 提交按钮 */
const submitForm = () => {
  orderFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateOrder(form.value).finally(() => buttonLoading.value = false);
      } else {
        await addOrder(form.value).finally(() => buttonLoading.value = false);
      }
      proxy?.$modal.msgSuccess("修改成功");
      dialog.visible = false;
      await getList();
    }
  });
}

/** 删除按钮操作 */
const handleDelete = async (row?: OrderVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除快递订单编号为"' + _ids + '"的数据项？').finally(() => loading.value = false);
  await delOrder(_ids);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('kd/order/export', {
    ...queryParams.value
  }, `order_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
  getList();
});
</script>
